<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="版本号" prop="versionNo">
        <el-input
          size="mini"
          v-model="queryParams.versionNo"
          placeholder="请输入版本号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:androidVersionMgt:add']"
        >新增版本</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" align="center" min-width="5%"/>
      <el-table-column label="版本号" align="center" prop="versionNo" key="versionNo"/>
      <el-table-column label="文件大小" align="center" prop="fileSize" key="fileSize"/>
      <el-table-column label="下载链接" align="center" :show-overflow-tooltip="true" prop="url" key="url"/>
      <el-table-column label="备注" align="center" :show-overflow-tooltip="true" prop="remark" key="remark"/>
      <el-table-column label="创建时间" align="center" prop="createAt">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateAt">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:androidVersionMgt:edit']"
          >修改</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleRemove(scope.row)"
            v-hasPermi="['system:androidVersionMgt:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="apk包：" prop="file">
          <fileUpload v-model="form.file" :file-size="100" :file-type="['apk']" :limit="1"/>
        </el-form-item>
        <el-form-item label="下载链接：" prop="url">
          <el-input disabled v-model="form.url"/>
        </el-form-item>
        <el-form-item label="文件大小(MB)：" prop="fileSize">
          <el-input disabled v-model="form.fileSize"/>
        </el-form-item>
        <el-form-item label="版本号："prop="versionNo">
          <el-input v-model="form.versionNo" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请填写版本备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <notice-member-list ref="notice-member-list"/>
  </div>
</template>
<script>
import { pageList,add,getInfo,edit,remove } from "@/api/triones/androidVersionMgt";
export default {
  name: "index",
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        versionNo: undefined
      },
      dataList: [],
      total: 0,
      dateRange: [],
      title: '',
      open: false,
      form: {},
      rules:{
        versionNo: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        url: [
          {required: true, message: "下载链接不能为空", trigger: "blur"}
        ],
        fileSize: [
          {required: true, message: "文件大小不能为空", trigger: "blur"}
        ],
      },
      sceneOptions: [],
      mainContentOptions: [],
      userOptions: [],
    }
  },
  watch: {
    'form.file': {
      handler(oldVal,newValue) {
        console.log('oldVal',oldVal)
        console.log('newValue',newValue)
        if (oldVal === undefined || oldVal === 'undefined') {
          this.$set(this.form,'url',undefined)
          this.$set(this.form,'fileSize',undefined)
        } else {
          this.$set(this.form,'url',oldVal.url)
          this.$set(this.form,'fileSize',(oldVal.fileSize / 1024 /1014).toFixed(2))
        }
      },
      deep: true
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(res => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 检索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getInfo(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.form.file = {
          url: response.data.url,
          fileSize: response.data.fileSize * 1024 * 1024,
        }
        this.open = true;
        this.title = "版本修改";
      });
    },
    handleAdd() {
      this.reset();
      this.title = "版本新增";
      this.open = true;
    },
    handleRemove(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除版本号为"' + row.versionNo + '"的数据项？').then(() => {
        this.loading = true;
        return remove(id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.$modal.confirm('是否确认该操作？').then(() => {
              edit(this.form).then(response => {
                this.getList()
                this.open = false
                this.$modal.msgSuccess("添加成功");
              });
            })
          } else {
            this.$modal.confirm('是否确认该操作？').then(() => {
              add(this.form).then(response => {
                this.getList()
                this.open = false
                this.$modal.msgSuccess("添加成功");
              });
            })
          }
        }
      });
    },
    cancel() {
      this.open = false;
    },
    // 表单重置
    reset() {
      this.form = {
        file: '',
        id: undefined,
        versionNo: undefined,
        url: undefined,
        remark: undefined,
        fileSize: undefined
      };
      this.resetForm("form");
    }
  }
}
</script>
<style scoped lang="scss">

</style>
